import logging
import time

from pages.basePage import BasePage
from config.gp_config import GpConfig
from tools.request import Base

from tools.logs import Logs

logger = Logs(__name__).get_logger()


class GpZtGy(BasePage):

    def __init__(self, sj_type):
        super().__init__()
        self.type_data = GpConfig.gp_zt_xpath.get(sj_type)
        self.type_data2 = GpConfig.gp_home_xpath.get(sj_type)
        self.request = Base()

    def click_more(self):
        # 点击更多按钮
        xpath = self.type_data.get("xpath").get("more")
        el = self.get_element_xpath(xpath)
        if el is not None:
            logger.info("存在更多按钮：{}，开始点击".format(el.text))
            el.click()

    def get_all_zt_gp(self):
        # 获取所有涨停的股票代码
        url = self.type_data.get("url")
        xpath = self.type_data.get("xpath").get("dm")
        # 先打开页面
        self.get(url)
        # 先点击
        self.click_more()
        # 再获取 存在延迟 等0.5秒
        time.sleep(0.5)
        html = self.get_page()
        gp_list = self.request.parse(html, xpath)
        return gp_list

    def get_bk_by_gp(self, gp_list):
        # 根据涨停的股票代码查询板块列表
        bk_list = []
        from pages.gp_homepage import GpHomePage
        gp = GpHomePage("gp")
        for code in gp_list:
            bks = gp.get_gp_bk(code)
            logger.info("当前股票id：{}， 所属板块：{}".format(code, bks))
            bk_list.extend(bks)
        return list(set(bk_list))

    def bk_is_ok(self, ex_l, ac_l):
        for ex in ex_l:
            if ex in ac_l:
                return True
            else:
                return False


if __name__ == '__main__':
    gp = GpZtGy("gp")
    re = gp.get_all_zt_gp()
    bk = gp.get_bk_by_gp(re)
    print(bk)
